#import "OmMomentsVC.h"
#import "OmMomentsVCTableViewCell.h"
#import "NoveltyApiNoveltyListModel.h"
#import "OmUploadMomentsVC.h"
#define CellReuseID @"OmMomentsVCTableViewCell"
@interface OmMomentsVC () <UITableViewDelegate, UITableViewDataSource, UIScrollViewRefreshDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation OmMomentsVC
#pragma mark - Event
- (void)refreshSelf {
    [self.tableView.mj_header beginRefreshing];
}
- (void)onPostButtonTap {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"member_api" A:@"translation"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        OmUploadMomentsVC *vc = [[OmUploadMomentsVC alloc] init];
        [self presentViewController:vc animated:YES completion:nil];
    } failure:^(NSString *error) {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"请升级皇冠会员") message:nil preferredStyle:UIAlertControllerStyleAlert];
        [controller addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"确认") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            OmBuyKingHatVC *vc = [[OmBuyKingHatVC alloc] initWithType:MemberTypeKingHat];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self presentViewController:controller animated:YES completion:nil];
    } hasCache:NO];
}
- (void)onMineButtonTap {
    OmMomentsVC *vc = [[OmMomentsVC alloc] init];
    vc.toUserID = curUser.id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - ConstructionScript
- (void)configSelf {
    [self setHiddenForTabBar:self.toUserID?YES:NO navigationBar:NO];
    if ([self.toUserID isEqualToString:curUser.id]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"发表") style:UIBarButtonItemStylePlain target:self action:@selector(onPostButtonTap)];
    } else {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:ASLocalizedString(@"我的新鲜事") style:UIBarButtonItemStylePlain target:self action:@selector(onMineButtonTap)];
    }
}
- (void)allocateComponents {
    self.tableView = (UITableView *)[[UITableView alloc] init]
    .fp_delegate(self)
    .fp_dataSource(self)
    .fp_separatorStyle(UITableViewCellSeparatorStyleNone)
    .fp_refreshPageCount(15)
    .fp_enableRefreshAndSetRefreshDelegate(self)
    .fp_backgroundColor(HexadecimalColor(@"#F2F2F2"));
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OmMomentsVCTableViewCell class]) bundle:nil] forCellReuseIdentifier:CellReuseID];
    [self.view addSubview:self.tableView];
}
- (void)layoutComponents {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}
#pragma mark - TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableView.refreshDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OmMomentsVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseID];
    NoveltyApiNoveltyListModel *model = tableView.refreshDataArray[indexPath.row];
    cell.isSelf = [self.toUserID isEqualToString:curUser.id];
    cell.model = model;
    cell.sd_indexPath = indexPath;
    cell.sd_tableView = tableView;
    return cell;
}
- (void)scrollView:(UIScrollView *)scrollView didBeginRefresh:(UIScrollView *(^)(NSMutableArray *))refreshData {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"novelty_list"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"uid"] = curUser.id;
    param[@"token"] = curUser.token;
    param[@"page"] = @(self.tableView.refreshPage);
    if (self.toUserID) param[@"to_user_id"] = self.toUserID;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        NSMutableArray *modelArray = [NoveltyApiNoveltyListModel mj_objectArrayWithKeyValuesArray:responseObject];
        refreshData(modelArray);
    } failure:^(NSString *error) {
    } hasCache:NO];
}
@end
